package com.lagou.filter;

import com.lagou.util.ResponseStatisticsUtil;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

@Activate(group = {CommonConstants.CONSUMER})
public class TPMonitorFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

        String methodName = invocation.getMethodName();

        long start = System.currentTimeMillis();
        try {
            return invoker.invoke(invocation);
        } finally {
            long spendTime = System.currentTimeMillis() - start;
//            System.out.println(methodName + ":" + spendTime);
            ResponseStatisticsUtil.saveMethodResponse(methodName, spendTime);
        }
    }
}
